<?php

/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/6/21
 * Time: 11:46
 */
class Passport extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('kdt_api_model');
    }

    public function login()
    {
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['wx_openid'])){
            $openid = $_SESSION['wx_openid'];
            $this->load->model('redis_model');
            $redis = $this->redis_model->getRedisInstance();
            $sls = $redis->get('seller:'.$openid);
            if ($sls===false) $sls='noauth';
            $_SESSION['sls'] = $sls;
            redirect('http://wap.nldou.com/salesmancenter/index.html');
        }else{redirect('http://api.nldou.com/ucenter/weixinlogin?pid=5328');}
    }

    public function loginv2()
    {
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['wx_openid'])){
            $openid = $_SESSION['wx_openid'];
            $this->load->model('redis_model');
            $redis = $this->redis_model->getRedisInstance();
            $sls = $redis->get('seller:'.$openid);
            if ($sls===false) $sls='noauth';
            $_SESSION['sls'] = $sls;
            redirect('http://wap.nldou.com/salesmancenter/v2/index.html');
        }else{redirect('http://api.nldou.com/ucenter/weixinlogin?pid=53281');}
    }

    public function logintest()
    {
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['wx_openid'])){
            $openid = $_SESSION['wx_openid'];
            $this->load->model('redis_model');
            $redis = $this->redis_model->getRedisInstance();
            $sls = $redis->get('seller:'.$openid);
            if ($sls===false) $sls='noauth';
            $_SESSION['sls'] = $sls;
            redirect('http://wap.nldou.com/onlinetestdir/v2/index.html');
        }else{redirect('http://api.nldou.com/ucenter/weixinlogin?pid=53282');}
    }

    public function checklogin()
    {
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['sls'])&&isset($_SESSION['wx_openid'])){
            $this->success(array('sls'=>$_SESSION['sls'],'openid'=>$_SESSION['wx_openid']));
        }else{
            $this->error('no session');
        }
    }

    public function firstlogin()
    {
        $status = array('status'=>1,'data'=>array());
        $mobile = $this->input->get('mobile');
        if (!isset($mobile)||empty($mobile)||!is_numeric($mobile)) $this->error();
        if(!isset($_SESSION)) session_start();
        if (!isset($_SESSION['wx_openid'])||!isset($_SESSION['uid'])){
            //验证信息不存在，跳转重新验证
            $status['status']=3;
            $this->success($status);
        }

        $res = $this->kdt_api_model->checkSalesman($mobile,$_SESSION['wx_openid']);
        //是销售员
        if ($res!==false){
            $data=array();
            if ($res['fans_id']!=0){
                $data['fans_id']=$res['fans_id'];
            }
            $data['idcode']=$res['seller'];
            $data['uid']=$_SESSION['uid'];
            $data['invifrom']=$res['from_buyer_mobile'];
            $data['phone']=$res['mobile'];
            $data['name']=$res['nickname'];
            $data['type']=preg_match('/PCFI/i',$res['nickname'])>0?'PCFI':'NLDOU';
            $data['created']=strtotime($res['created_at']);

            $this->load->model('kdtsalesman_model');
            //同步销售员
            if ($this->kdtsalesman_model->syncsalesman($data)){
                $this->load->model('redis_model');
                $redis = $this->redis_model->getRedisInstance();
                $redis->set('seller:' . $_SESSION['wx_openid'], $res['seller']);//绑定销售员
                $_SESSION['sls'] = $res['seller'];//更新session
            }

            $status['data']['openid']=$_SESSION['wx_openid'];
            $status['data']['sls']=$data['idcode'];
        }else{
            //不是销售员,跳转申请页
            $status['status']=2;
        }
        $this->success($status);
    }

    public function authcallback()
    {
        $this->kdt_api_model->authcallback();
    }

    public function getInfo()
    {
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['wx_openid'])){
            $openid=$_SESSION['wx_openid'];
        }else{$this->error();}

        //获取用户名和头像
        $this->load->model('user_model');
        $user=$this->user_model->getUser(array('wx_openid'=>$openid),'userIcon,userName');
        if (empty($user)) $this->error();
        //获取代言人手机
        $sls=$this->input->get('sls');
        if (!isset($sls)||empty($sls)) $this->error();
        $this->load->model('kdtsalesman_model');
        $phone=$this->kdtsalesman_model->getSalesman($sls);
        if ($phone===false) $this->error();
        //获取代言人成交额
        $res=$this->kdt_api_model->getSalesmanAccount($phone);
        $user['money']=(float)$res['money'];

        $user['sls']=$sls;

        $this->success($user);
    }

    public function getUserInfo()
    {
        $openid = $this->input->post('wx_openid');
        if (!isset($openid)||empty($openid)) $this->error();
//        $openid="oHlcTt7k0U-w5QaTvRqqvDQvfOQQ";
        $this->load->model('user_model');
        $res=$this->user_model->getUser(array('wx_openid'=>$openid),'userIcon,userName');
        if (empty($res)) $this->error();
        $this->success($res);
    }

    public function getUserProfit()
    {
        $sls=$this->input->post('sls');
        if (!isset($sls)||empty($sls)) $this->error();
//        $mobile='18321136597';
        $this->load->model('kdtsalesman_model');
        $mobile=$this->kdtsalesman_model->getSalesman($sls);
        if ($mobile===false) $this->error();//代言人不存在

        $res = $this->kdt_api_model->getSalesmanAccountScore($mobile);
        if ($res===false) $this->error();//有赞接口错误
        //佣金
        $wait_settle_money=round((float)$res['accumulations'][0]['wait_settle_money'],2);
        $settle_money=round((float)$res['accumulations'][0]['settle_money'],2);
        $profit = $settle_money+$wait_settle_money;
        //成交额
        $manual_settle_order_amount=round((float)$res['accumulations'][0]['manual_settle_order_amount'],2);
        $auto_settle_order_amount=round((float)$res['accumulations'][0]['auto_settle_order_amount'],2);
        $orderamount = $manual_settle_order_amount+$auto_settle_order_amount;
        $this->success(array('profit'=>$profit,'orderamount'=>$orderamount));
    }

    public function getUserCustomerNum()
    {
        $sls=$this->input->post('sls');
        if (!isset($sls)||empty($sls)) $this->error();
//        $mobile='18321136597';
        $this->load->model('kdtsalesman_model');
        $mobile=$this->kdtsalesman_model->getSalesman($sls);
        if ($mobile===false) $this->error();//代言人不存在

        $res = $this->kdt_api_model->getSalesmanCustomers($mobile,1,1);
        if ($res===false) $this->error();//有赞接口错误

        $this->success(array('cusnum'=>$res['total_results']));
    }
    public function getCustomerList()
    {
        $sls=$this->input->get('sls');
        $pageno=$this->input->get('pageno');
        $pagesize=$this->input->get('pagesize');
        if (!isset($sls)||empty($sls)) $this->error();
        if (!isset($pageno)||empty($pageno)||!is_numeric($pageno)) $pageno=1;
        if (!isset($pagesize)||empty($pagesize)||!is_numeric($pagesize)) $pagesize=20;

        $this->load->model('kdtsalesman_model');
        $list=$this->kdtsalesman_model->getSlsCustomerList($sls,$pageno,$pagesize);
        if ($list===false) $this->error();
        $this->success($list);
    }
    public function getUserInviNum()
    {
        $sls=$this->input->post('sls');
        if (!isset($sls)||empty($sls)) $this->error();
//        $mobile='18321136597';
        $this->load->model('kdtsalesman_model');
        $mobile=$this->kdtsalesman_model->getSalesman($sls);
        if ($mobile===false) $this->error();//代言人不存在

        $res = $this->kdtsalesman_model->getInviSalesmanNum($mobile);

        $this->success(array('invinum'=>$res));
    }
    public function getUserTradesNum()
    {
        $sls=$this->input->post('sls');
        if (!isset($sls)||empty($sls)) $this->error();
//        $mobile='18321136597';
        $this->load->model('kdtsalesman_model');
        $mobile=$this->kdtsalesman_model->getSalesman($sls);
        if ($mobile===false) $this->error();//代言人不存在

        $res = $this->kdt_api_model->getSalesmanTrades($mobile,1,1);
        if ($res===false) $this->error();//有赞接口错误

        $this->success(array('tradesnum'=>$res['total_results']));
    }

    public function changeIcon()
    {
        $post = $this->input->post();
        if (!isset($post['sls'])||empty($post['sls'])) $this->error('lack sls');
        else $sls = trim($post['sls']);
        if (!isset($post['mediaid'])||empty($post['mediaid'])) $this->error('lack mediaid');
        else $mediaid = $post['mediaid'];

        $this->config->load('weixin');
        $appid = $this->config->item('nldou_fuwu_appid');
        $appsecret = $this->config->item('nldou_fuwu_appsecret');
        $wxtype = $this->config->item('nldou_fuwu_type');

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$appid,"appsecret"=>$appsecret,"wxtype"=>$wxtype));
        $res = $this->wxinterface->download_temp_image(array($mediaid));

        $this->load->model('salesman_post_model');
        $config['source_image'] = $res[0]['filepath'];
        $config['width']     = '128';
        $config['height']   = '128';
        $config['maintain_ratio'] = true;
        $config['master_dim'] = 'auto';
        $this->salesman_post_model->resize($config);
        $this->salesman_post_model->changeUserIcon($sls,$res[0]['dbpath']);
        $this->success($res[0]);
    }
}